AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

util.PrecacheSound( "sfls3/hover.wav" )

include('shared.lua')

local Increment = 1000000
local phys = self.Entity:GetPhysicsObject();

function ENT:Initialize()
	self.Entity:SetColor(5, 30, 5, 255)
	self.Entity:SetMaterial("spacebuild/Fusion4")
	phys:EnableDrag(true)
	self.BaseClass.Initialize(self)
	self.Active = 0
	self.overdrive = 0
	self.damaged = 0
	self.lastused = 0
	self.thinkcount = 0
	self.Mute = 0
	self.Multiplier = 1
	self.loopingSound = CreateSound(self.Entity, "sfls3/hover.wav" )
	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Inputs = Wire_CreateInputs(self.Entity, { "On", "Overdrive", "Mute", "Multiplier" })
		self.Outputs = Wire_CreateOutputs(self.Entity, {"On", "Overdrive"})
	else
		self.Inputs = {{Name="On"}}
	end

end

function ENT:TurnOn()
	if (self.Active == 0) then
		if (self.Mute == 0) then
			repeat self.loopingSound:PlayEx(0.60, 85) until (self.Active == 0 or self.Mute ==1)
		end
		self.Active = 1
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", self.Active) end
		self:SetOOO(1)
	elseif ( self.overdrive == 0 ) then
		self:TurnOnOverdrive()
	end
	self.Entity:SetColor(5, 30, 5, 255)
	self.Entity:SetMaterial("spacebuild/Fusion4")
end

function ENT:TurnOff()
	if (self.Active == 1) then
		if (self.Mute == 0) then self.loopingSound:Stop() end
		self.Active = 0
		self.overdrive = 0
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", self.Active) end
		self:SetOOO(0)
	end
	self.Entity:SetColor(5, 30, 5, 255)
	self.Entity:SetMaterial("spacebuild/Fusion4")
end

function ENT:TurnOnOverdrive()
	if ( self.Active == 1 ) then
		self.overdrive = 1
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Overdrive", self.overdrive) end
	end
end

function ENT:TurnOffOverdrive()
	if ( self.Active == 1 and self.overdrive == 1) then
		self.overdrive = 0
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Overdrive", self.overdrive) end
	end	
end

function ENT:SetActive( value )
	if (value) then
		if (value != 0 and self.Active == 0 ) then
			self:TurnOn()
		elseif (value == 0 and self.Active == 1 ) then
			self:TurnOff()
		end
	else
		if ( self.Active == 0 ) then
			self.lastused = CurTime()
			self:TurnOn()
		else
			if ((( CurTime() - self.lastused) < 2 ) and ( self.overdrive == 0 )) then
				self:TurnOnOverdrive()
			else
				self.overdrive = 0
				self:TurnOff()
			end
		end
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		self:SetActive(value)
	elseif (iname == "Overdrive") then
		if (value ~= 0) then
			self:TurnOnOverdrive()
		else
			self:TurnOffOverdrive()
		end
	end
	if (iname == "Mute") then
		if (value > 0) then
			self.Mute = 1
		else
			self.Mute = 0
		end
	end
	if (iname == "Multiplier") then
		if (value > 0) then
			self.Multiplier = value
		else
			self.Multiplier = 1

		end	
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
	if ((self.Active == 1) and (math.random(1, 10) <= 4)) then
		self:TurnOff()
	end
	self.Entity:SetColor(5, 30, 5, 255)
	self.Entity:SetMaterial("spacebuild/Fusion4")
end

function ENT:Repair()
	self.BaseClass.Repair(self)
	self.Entity:SetColor(5, 30, 5, 255)
	self.Entity:SetMaterial("spacebuild/Fusion4")
	self.damaged = 0
end

function ENT:Destruct()
	if CAF and CAF.GetAddon("Life Support") then
		CAF.GetAddon("Life Support").Destruct( self.Entity, true )
	end
end

function ENT:OnRemove()
	self.BaseClass.OnRemove(self)
	self.loopingSound:Stop()
	if ValidEntity(phys) then
		phys:EnableGravity(true);
	end

end

function ENT:Bhc()
	if ValidEntity(phys) then
		phys:EnableGravity(false);
	end
	local energy = self:GetResourceAmount("energy")
	local inc = math.Round(((Increment + (self.overdrive * Increment )) * self:GetMultiplier()) * self.Multiplier) * 1024
	if (inc >= math.Round(inc/2)) then
		if ( self.overdrive == 1 ) then
			if CAF and CAF.GetAddon("Life Support") then
				CAF.GetAddon("Life Support").DamageLS(self, math.random(2, 3))
			else
				self:SetHealth( self:Health( ) - math.Random(2, 3))
				if self:Health() <= 0 then
					self:Remove()
				end
			end
		end
		self:SupplyResource("energy", inc)
		self:SupplyResource("water", inc)
		self:SupplyResource("oxygen", inc)
		self:SupplyResource("nitrogen", inc)
		self:SupplyResource("hydrogen", inc)
		self:SupplyResource("heavy water", inc)
		self:SupplyResource("steam", inc)
		self:SupplyResource("liquid nitrogen", inc)
		self:SupplyResource("carbon dioxide", inc)
		self:SupplyResource("ZPE", inc)
		self:SupplyResource("ZPH", inc)
		self:SupplyResource("coolant", inc)
		self:SupplyResource("naquadah", inc)
	else
		self:TurnOff()
	end
end

function ENT:Think()
	self.BaseClass.Think(self)
	self.thinkcount = self.thinkcount + 1
	if self.thinkcount == 10 then
		if ( self.Active == 1 ) then self:Bhc() end
		self.thinkcount = 0
	end
	self.Entity:NextThink( CurTime() + 0.1 )
	return true
end

